﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SiteRecord
{
    public class SecurityBase: System.Web.UI.Page
    {
        public static SiteRecordDataContext SiteRecordContext = new SiteRecordDataContext();

        public bool TokenIsOk
        {
            get
            {
                if (!Request.Form["username"].EToString().IsNullOrEmpty())
                {
                    CurrentUser = SiteRecordContext.Users.FirstOrDefault(p => p.Name == Request.Form["username"].EToString());
                }

                return CurrentUser != null;
            }
        }

        public string UserName
        {
            get
            {
                if (TokenIsOk)
                {
                    Session["UserName"] = Request.Form["username"].EToString();
                }
                else
                    Session["UserName"] = string.Empty;

                return Session["UserName"].EToString();
            }
        }

        public Users CurrentUser
        {
            get { return (Users)Session["CurrentUser"]; }

            set { Session["CurrentUser"] = value; }
        }

        public void Update()
        {
            SiteRecordContext = new SiteRecordDataContext();
            CurrentUser = SiteRecordContext.Users.FirstOrDefault(p => p == CurrentUser);
        }

        protected override void OnPreRender(EventArgs e)
        {
          
            base.OnPreRender(e);
       
            //Page.RegisterScript(String.Format("DebugInfo('name:{0}-pwd:{1}');", Request.Form["username"].EToString(), Request.Form["userpwd"].EToString()));
        }

    }
}